//
//  BuyInfoScrollViewTableViewCell.m
//  EasyBuy
//
//  Created by apple on 12-10-20.
//  Copyright (c) 2012年 shen donghao. All rights reserved.
//

#import "BuyInfoScrollViewTableViewCell.h"

@implementation BuyInfoScrollViewTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        BuyInfoData *buyInfoData = [[BuyInfoData alloc] init];
//        imageCount = [buyInfoData.buyPhotosArray count];
        imageCount = 2;
        [buyInfoData release];
        
        // Initialization code
        
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0., 0., self.frame.size.width, 200.)];
        CGSize newSize = CGSizeMake(240*imageCount, 180);
        [_scrollView setContentSize:newSize];
        _scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"commit_bg"]];
        [_scrollView setPagingEnabled:YES];
        [_scrollView setShowsHorizontalScrollIndicator:NO];
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 190,self.frame.size.width, 10)];
        [_pageControl setHidesForSinglePage:YES];
        [_pageControl setUserInteractionEnabled:YES];
//        [_pageControl setBackgroundColor:[UIColor blueColor]];
        [_pageControl setNumberOfPages:imageCount];
        [_pageControl setCurrentPage:0];
        [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_pageControl];
    }
    return self;
}

- (void)initCellWithBuyScrollInfoData:(BuyInfoData *)buyInfoData{
     
    for (int i = 0; i < imageCount; ++i) {
        UIImageView *indexImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"35"]];
//        NSString *str = [NSString stringWithFormat:@"%@",[buyInfoData.buyPhotosArray indexOfObject:i]];
//        UIImageView *indexImageView = [[UIImageView alloc]initWithImage:[NSString stringWithFormat:@"%@",str]];
        indexImageView.frame = CGRectMake(40, 10, 240., 180.);
        [_scrollView addSubview:indexImageView];
        [indexImageView release];
        UIImageView *indexImageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"33"]];
        indexImageView1.frame = CGRectMake(280, 10, 240., 180.);
        [_scrollView addSubview:indexImageView1];
        [indexImageView1 release];
    }
}

//通过pageControl去改变scroll的显示内容
- (void)changePage:(id)sender{
    int page = [sender currentPage];
    CGRect frame = self.frame;
    frame.origin.x = frame.size.width*page;
    frame.origin.y = 0;
    [_scrollView scrollRectToVisible:frame animated:YES];
}

//滚动结束 点移动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollViews{
    int pageNum = fabs(_scrollView.contentOffset.x/_scrollView.frame.size.width);
    [_pageControl setCurrentPage:pageNum];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
